package net.sourceforge.peers;


import java.net.*;



import net.sourceforge.peers.media.MediaMode;
import net.sourceforge.peers.sip.syntaxencoding.SipURI;
import net.sourceforge.peers.sip.syntaxencoding.SipUriSyntaxException;



import org.testng.annotations.Test;

import javax.xml.crypto.Data;

public  class XmlConfigTestNG {





    @Test
    public void testSave() throws SipUriSyntaxException, UnknownHostException {
        String fileName = getClass().getResource("configTest.xml").getFile();
        Logger logger = new FileLogger(null);
        Config config = new XmlConfig(fileName, logger);
        InetAddress localHost = InetAddress.getLocalHost();
        String userPart = "alice";
        String domain = "sourceforge.net";
        String password = "123";
        SipURI sipURI = new SipURI("sip:sourceforge.net;lr");
        int sipPort = 6060;
        MediaMode mediaMode = MediaMode.echo;
        boolean mediaDebug = true;
        int rtpPort = 8002;
        String authorizationUsername = "authUser";

        config.setLocalInetAddress(localHost);
        config.setUserPart(userPart);
        config.setDomain(domain);
        config.setPassword(password);
        config.setOutboundProxy(sipURI);
        config.setSipPort(sipPort);
        config.setMediaMode(mediaMode);
        config.setMediaDebug(mediaDebug);
        config.setRtpPort(rtpPort);
        config.setAuthorizationUsername(authorizationUsername);
        config.save();
        config = new XmlConfig(fileName, logger);
        assert localHost.equals(config.getLocalInetAddress());
        assert userPart.equals(config.getUserPart());
        assert domain.equals(config.getDomain());
        assert password.equals(config.getPassword());
        assert sipURI.toString().equals(config.getOutboundProxy().toString());
        assert sipPort == config.getSipPort();
        assert mediaMode == config.getMediaMode();
        assert mediaDebug == config.isMediaDebug();
        assert rtpPort == config.getRtpPort();
        assert authorizationUsername.equals(config.getAuthorizationUsername());
    }

    @Test
    public void t1()
    {

        var a="s";
        var b=1;
        System.out.println(a.getClass().getName());

    }

//    @Test
//    public void t2() throws IOException, InterruptedException {
//        String address = getAddressByUsername("102");
//        System.out.println(address);
//
//
//
//    }

//    private String getAddressByUsername(String username) throws IOException, InterruptedException {
//
//
//        String cookie = getCookie();
//
//        var request = HttpRequest.newBuilder()
//                .uri(URI.create("http://124.223.170.227:8080/dbajax/localuser/list"))
//                .header("Cookie",cookie)
//                .GET()
//                .build();
//        var client = HttpClient.newHttpClient();
//
//        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//
//        String body = response.body();
//        String begin="{\"data\":";
//        String end="}";
//        String data=begin+body+end;
//        JSONObject jsonObject = JSON.parseObject(data);
//        JSONArray error = jsonObject.getJSONArray("data");
//        List<Account> accounts = JSON.parseObject(error.toJSONString(), new TypeReference<List<Account>>() {
//        });
//        for (Account account : accounts) {
//            if(account.getUsrname().equals(username) & !account.getAddr().isBlank()){
//                return account.getAddr();
//            }
//        }
//        return "null";
//    }
//
//    private String getCookie() throws IOException, InterruptedException {
//        var request = HttpRequest.newBuilder()
//                .uri(URI.create("http://124.223.170.227:8080/authorize?password=admin"))
//                .GET()
//                .build();
//        var client = HttpClient.newHttpClient();
//
//        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//        HttpHeaders headers = response.headers();
//        Optional<String> cookie  = headers.firstValue("set-cookie");
//        String s = cookie.get();
//        String[] split = s.split(";");
//        return split[0];
//    }

    @Test
    public void sound()
    {


    }




}


